
import {
  getProcessTypeIdList,
  getYesOrNotList,
  getActiveStatusList,
  getReportModelList,
  saveProcessDetail,
  getLineCodeListNew,
  baseFactoryModeling,
} from "@/api/process/mesProcess";

import {
  mesSeriesList,
  rowCreate,
  rowDetail,
  upgrade,
  upgradeNew,
  mesTechnologyUpdate,
} from "@/api/process/mesTechnology";
import { checkStr } from "@/utils/judge";

export const config = {
  data() {
    const _this = this;
    var validatePass = (rule, value, callback) => {
      setTimeout(() => {
        if (!checkStr(value, "english-number")) {
          callback(new Error($i18n.t("mesTechnology.rule8")));
        } else {
          callback();
        }
      }, 100);
    };
    return {
      // 基础信息 表单信息
      formData: {},
      mesSeriesList: [],
      formConfig: {
        span: 24,
        disabled: false,
        labelPosition: "right",
        labelWidth: "160px",
        isShowSubmitBtn: true,
        rules: {
          technologyCode: [
            { validator: validatePass, trigger: ["change", "blur"] },
          ],
        },
        formDesc: {
          technologyType: {
            layout: 24,
            type: "radio",
            label: "",
            required: true,
            default: "product",
            disabled: (d) => !!d.id,
            options: [
              {
                text: $i18n.t("mesTechnology.specifiedProduct"),
                value: "product",
              },
              {
                text: $i18n.t("mesTechnology.productCategory"),
                value: "series",
              },
            ],
            on: {
              change() {
                _this.formData.prodCode = "";
                _this.formData.seriesCode = "";
                _this.formData.seriesName = "";
              },
            },
          },


          prodName: {
            layout: 12,
            type: "input",
            label: $i18n.t("mesTechnology.specifiedProduct"),
            required: true,
            disabled: (d) => !!d.id,
            attrs: { readonly: true },
            slots: {
              prepend(h, data) {
                return (
                  <el-button>
                    {_this.formData.prodCode || $i18n.t("mesTechnology.none")}
                  </el-button>
                );
              },
              // 第一个参数是渲染函数，第二个参数是作用域插槽的数据
              append(h) {
                return (
                  <el-button
                    slot="append"
                    onClick={() => _this.rightDialog()}
                    icon="el-icon-search"
                  ></el-button>
                );
              },
            },
            // 当 data.girlfriend 为 true 时显示
            vif(data) {
              return data.technologyType == "product";
            },
          },

          prodCode1: {
            //字段需要特殊处理
            layout: 12,
            type: "select",
            label: $i18n.t("mesTechnology.productCategory"),
            required: true,
            attrs: {
              filterable: true,
            },
            disabled: (d) => !!d.id,
            prop: {
              value: "seriesCode",
              text: "seriesName",
            },
            options: async (data) => {
              const list = await mesSeriesList();
              this.mesSeriesList = list.data.result;
              return list.data.result;
            },
            vif(data) {
              return data.technologyType == "series";
            },
            on: {
              change: (val) => {
                if (val) {
                  const selected = this.mesSeriesList.find((item) => item.seriesCode == val)
                  this.formData.seriesName = selected.seriesName
                  this.formData.seriesCode = val
                }
              }
            }
          },
          // 系列编码
          seriesCode: {
            type: "input",
            label: $i18n.t("mesTechnology.seriesCode"),
            layout: 12,
            required: false,
            disabled: true,
          },
          technologyCode: {
            type: "input",
            label: $i18n.t("mesTechnology.technologyCode"),
            layout: 12,
            disabled: (d) => !!d.id,
            required: true,
          },
          // 系列名称
          seriesName: {
            type: "input",
            label: $i18n.t("mesTechnology.seriesName"),
            layout: 12,
            required: false,
            disabled: true,
          },
          technologyName: {
            type: "input",
            label: $i18n.t("mesTechnology.technologyName"),
            layout: 12,
            required: true,
          },
          versionNumber: {
            layout: 12,
            type: "input",
            label: $i18n.t("mesTechnology.versionNumber"),
            disabled: true,
            default: "A01",
          },
          workshopCode: {
            type: "select",
            label: $i18n.t("mesTechnology.workshopCode"),
            required: true,
            layout: 12,
            //disabled: (d) => !!d.id,
            attrs: {
              filterable: true,
            },
            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            options: async (data) => {
              const params = {
                modelingCode:this.$store.getters.factoryCode,
                level: 4,
                modelingType: "0",
              };
              const list = await getLineCodeListNew(params);
              this.workshopList = list.data.result;
              return list.data.result;
            },
            vif: (data) => {
              return data.isDefaultTechnology == "1";
            },
            on: {
              change(val) {
                _this.workshopList.forEach((v) => {
                  if (v.modelingCode == val)
                    _this.formData.workshopName = v.modelingName;
                });
              },
            },
          },

          isDefaultTechnology: {
            type: "select",
            label: $i18n.t("mesTechnology.isDefaultTechnology"),
            layout: 12,
           // disabled: (d) => !!d.id,
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getYesOrNotList();
              return list.data.result;
            },
          },

          text1: {
            layout: 12,
            type: "text",
            label: "",
            opacity : 1,
            attrs:{
              opacity:1
            },
            vif: (data) => {
              return data.isDefaultTechnology != "1";
            },
          },
          remark: {
            type: "textarea",
            label: $i18n.t("mesTechnology.announcements"),
            layout: 12,
          },
        },
      },
      WorkingFormData: {},
      WorkingFormConfig: {
        span: 24,
        disabled: false,
        labelPosition: "right",
        //  labelWidth: "100px",
        isShowSubmitBtn: true,
        formDesc: {
          // 字段已对应
          processCode: {
            type: "input",
            label: $i18n.t("mesTechnology.processCode"),
            layout: 12,
            disabled: true,
            required: true,
          },
          processTypeId: {
            layout: 12,
            type: "select",
            label: $i18n.t("mesTechnology.processTypeId"),
            disabled: true,
            prop: {
              value: "id",
              text: "processTypeName",
            },
            options: async (data) => {
              const list = await getProcessTypeIdList();
              return list.data.result;
            },
          },
          processName: {
            type: "input",
            label: $i18n.t("mesTechnology.processName"),
            layout: 12,
            disabled: true,
            required: true,
          },
          isReportStart: {
            type: "select",
            label: $i18n.t("mesTechnology.isReportStart"),
            layout: 12,
            required: false,
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getYesOrNotList();
              return list.data.result;
            },
          },
          endReportModel: {
            type: "select",
            label: $i18n.t("mesTechnology.endReportModel"),
            layout: 12,
            required: false,
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getReportModelList();
              return list.data.result;
            },
          },
          workshopCode: {
            type: "select",
            label: $i18n.t("mesTechnology.workshopCode"),
            layout: 12,
            disabled: true,
            attrs: {
              filterable: true,
            },
            prop: {
              value: "modelingCode",
              text: "modelingName",
            },
            options: async (data) => {
              const params = {
                modelingCode:this.$store.getters.factoryCode,
                level: 4,
                modelingType: "0",
              };
              const list = await baseFactoryModeling(params);
              this.workshopList = list.data.result;
              return list.data.result;
            },
            on: {
              change: (val) => {
                // debugger;
                if (!val) {
                  self.$set(self.formDataDialog, "workshopName", "");
                } else {
                  let obj = self.workshopList.find((item) => {
                    return item.modelingCode == val;
                  });
                  self.$set(self.formDataDialog, "workshopName", obj.modelingName);
                }
              },
            },
          },
          reportOrNot: {
            type: "select",
            label: $i18n.t("mesTechnology.reportOrNot"),
            layout: 12,
            required: false,
            disabled: false,
            prop: {
              value: "value",
              text: "description"
            },
            attrs: {
              clearable: false
            },
            options: async (data) => {
              const list = await getYesOrNotList()
              return list.data.result
            }
          },
          processContent: {
            type: "textarea",
            label: $i18n.t("mesTechnology.processContent"),
            layout: 12,
            disabled: true,
          },
          remark: {
            type: "textarea",
            label: $i18n.t("common.remark"),
            disabled: true,
            layout: 12,
          },
          activeStatus: {
            type: "select",
            label: $i18n.t("mesTechnology.activeStatus"),
            layout: 12,
            required: false,
            disabled: false,
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getActiveStatusList();
              return list.data.result;
            },
          },
        },
      },
    };
  },
  methods: {
    /**
     * @description /   基础信息详情
     * @author bing
     * @date 2022-03-16 17:15
     * @param {*} id
     * @param {function} callback 回调
     */
    async rowBaseDetail(id, callback) {
      let res = await rowDetail(id);
      const { technologyFile } = res.data.result;
      const technologyFiles = technologyFile
        ? technologyFile.split(",").map((val) => {
          const arr = val.split("/");
          return {
            url: val,
            name: arr[arr.length - 1],
          };
        })
        : [];
      this.formData = {
        ...res.data.result,
        prodCode1: res.data.result.prodCode || res.data.result.seriesCode,
        technologyFile: technologyFiles,
      };

      callback();
      //   if (type == 3) {

      //   }
    },

    /**
     * @description   //基础信息提交 新增或者修改
     * @author bing
     * @date 2022-03-16 17:15
     * @param {*} data
     */
    handleSubmit(data) {
      this.$refs["formData"].validate().then(async () => {
        try {
          // eslint-disable-next-line no-console
          if (data.technologyFile) {
            data.technologyFile = data.technologyFile
              .map((r) => r.url)
              .join(",");
          }
          if (data.technologyType == "series") {
            if (!data.prodCode1) data.prodCode = data.prodCode1; //产品类别重置到指定字段
          }
          if (data.id) {
            let res = await mesTechnologyUpdate(data);

            this.id = res.data.result;

            this.setactiveNameApi("2");
            this.type = 2;
            this.$message.success($i18n.t("mesTechnology.updateSuccess2"));
          } else {
            console.log("rowCreate传参：", data)
            let res = await rowCreate(data);
            if (res.data.result != "0") {
              //升级
              this.$confirm(
                `${$i18n.t("mesTechnology.confirm", { name: data.technologyName })}`,
                $i18n.t("common.tip"),
                {
                  confirmButtonText: $i18n.t("common.ok"),
                  cancelButtonText: $i18n.t("common.cancel"),
                  type: "warning",
                }
              ).then(() => {
                data.id = res.data.result
                upgradeNew(data).then((ress) => {
                  this.type = 2;
                  this.$message.success($i18n.t("mesTechnology.updateSuccess"));
                  this.formData = ress.data.result;
                });
              });
            } else {
              this.id = res.data.result;

              this.setactiveNameApi("2");
              this.type = 2;
              this.$message.success($i18n.t("common.addSuccess2"));
            }
          }
        } catch (error) { }
      });
    },
    handleSuccess() {
      //this.$message.success("创建成功");
    },
    /**
     * @description //基本信息  打开侧边弹窗
     * @author bing
     * @date 2022-03-16 17:15
     */
    rightDialog() {
      if (!!this.formData.id) {
        return
      }

      this.$refs.baseDialog.drawer = true;
    },
    //基本信息  选择产品
    resBaseDialog(data) {
      this.$set(this.formData, "prodName", data[0].goodsName);
      this.$set(this.formData, "prodCode", "");
      this.$set(this.formData, "prodCode", data[0].goodsCode);
      this.$set(this.formData, "seriesCode", data[0].seriesCode);
      this.$set(this.formData, "seriesName", data[0].seriesName);
      console.log(111, data);
    },
    //  加工顺序 添加
    resWorkingDialog(data) {
      let list = this.list;
      console.log(111, list);
      for (let i in data) {
        let item = data[i];
        list.push({
          ...item,
          code: item.processCode,
          name: item.processName,
          processId: item.id,
        });
      }
      console.log(111, list);
      this.list = this.unique(list, "processId");
    },

    //提交
    async handleSubmitWorking(data) {
      data.technologyCode = this.formData.technologyCode;
      data.technologyName = this.formData.technologyName;
      data.versionNumber = this.formData.versionNumber;

      try {
        await saveProcessDetail(data);
        this.$message.success($i18n.t("common.editSuccess2"));
        console.log('this.tabPosition',this.tabPosition)
        this.setRightBase(this.tabPosition);
      } catch (error) {

      }


    },
    handleSuccessWorking() {
      debugger;
      // this.$message.success($i18n.t("common.editSuccess2"));
      // this.setRightBase(this.tabPosition);
    },
    //加工顺序  打开
    isShowWorkingDialog() {
      this.$refs.workingDialog.drawer = true;
    },

    /**
     * @description  数组去重
     * @author bing
     * @date 2022-03-16 17:16
     * @param {Array} arr
     * @param {String} key
     * @returns {*}
     */
    unique(arr, key) {
      const map = new Map();
      return arr.filter(
        (item) => !map.has(item[key] + "") && map.set(item[key] + "", 1)
      );
    },

    handleSelect(val, field, options) {
      if (!val) {

      } else {
        const selected = options.find((item) => item.seriesCode == val)
        this.formConfig.formDesc.seriesName = selected.seriesName
        this.formConfig.formDesc.seriesCode = val
      }
    }
  },
};
